﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BabyToys.Models;
using PagedList;
using PagedList.Mvc;
namespace BabyToys.Controllers
{
    public class SanPhamController : Controller
    {
        //
        // GET: /SanPham/
        DatabaseContext db = new DatabaseContext();
        public void LoadSanPham(int id, List<SanPham> sanphams)
        {
            var dsdanhmuc = db.DanhMucs.Where(d => d.DanhMucCha.IdDanhMuc == id);
            if (dsdanhmuc != null)
            {
                foreach (var danhmuc in dsdanhmuc)
                {
                    var dssanpham = db.SanPhams.Where(b => b.IdDanhMuc == danhmuc.IdDanhMuc);
                    if (dssanpham != null)
                    {
                        foreach (var bv in dssanpham)
                        {
                            sanphams.Add(bv);
                        }
                    }
                    LoadSanPham(danhmuc.IdDanhMuc, sanphams);
                }
            }
        }
        public ActionResult Index()
        {
            var sanphams = db.SanPhams.ToList().OrderByDescending(s => s.IdSanPham).Take(12);
            return View(sanphams);
        }
        
        public ActionResult SanPhamTheoDanhMuc(int id, int? page, int? hienthi,int? sapxep)
        {
            var danhmuc = db.DanhMucs.SingleOrDefault(d => d.IdDanhMuc == id);
            ViewBag.Id = danhmuc.IdDanhMuc;
            ViewBag.Ten = danhmuc.TenDanhMuc;
            var sanphams = db.SanPhams.Where(s => s.IdDanhMuc == id).ToList();
            LoadSanPham(id, sanphams);
            sanphams = sanphams.OrderByDescending(s => s.IdSanPham).ToList();
            switch (sapxep ?? 0)
            {
                case 0: break;
                case 1:
                    sanphams = sanphams.OrderBy(s => s.Giaban).ToList();
                    break;
                case 2:
                    sanphams = sanphams.OrderByDescending(s => s.Giaban).ToList();
                    break;
                case 3:
                    sanphams = sanphams.OrderBy(s => s.TenSP).ToList();
                    break;
                case 4:
                    sanphams = sanphams.OrderByDescending(s => s.TenSP).ToList();
                    break;
                default:
                    break;
            }
            var pageNumber = page ?? 1;
            ViewBag.onePageOfSanPham = sanphams.ToPagedList(pageNumber, hienthi ?? 9);
            ViewBag.HienThi = hienthi ?? 10;
            ViewBag.SapXep = sapxep ?? 0;
            
            return View();
        }
        public ActionResult ChiTiet(int id=0)
        {
            var sanpham = db.SanPhams.SingleOrDefault(s => s.IdSanPham == id);
            
            ViewBag.ChiTietHinh = db.HinhAnhs.ToList().Where(h => h.SanPham == sanpham);
           // ViewBag.LoaiSP = db.DanhMucs.Where(loai => loai.IdDanhMuc == sanpham.IdDanhMuc);
           // ViewBag.ThuongHieu = db.ThuongHieus.Where(th => th.IdThuongHieu == sanpham.IdThuongHieu);

            return View(sanpham);
        }
    }
}
